package org.jsbd.boss.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.statistics.UseStatisticsDto;
import org.jsbd.boss.service.IBaseInfoService;
import org.jsbd.boss.service.IUseStatisticsService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/use")
public class UseStatisticsController {

	private static final Logger logger = LoggerFactory.getLogger(UseStatisticsController.class);

	@Autowired
	private IUseStatisticsService useStatisticsService;
	@Resource
	private IBaseInfoService baseInfoService;

	/**
	 * 按基地统计列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/base/list")
	public String baseList(Model model) {
		List<BaseInfo> baseInfos = baseInfoService.getAllChannelInfo();
		model.addAttribute("baseInfos", baseInfos);
		return "useStatistics/baseList";
	}

	/**
	 * 
	 * 按基地统计列表数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/base/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> baseListJson(DataGridModel page, UseStatisticsDto usDTO) {
		String startTime = usDTO.getStartTime();
		String endTime = usDTO.getEndTime();
		if (!StringUtils.isBlank(startTime)) {
			usDTO.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			usDTO.setEndTime(endTime.replace("-", ""));
		}
		return useStatisticsService.findBaseListWithPage(page, usDTO);
	}

	/**
	 * 按省份统计列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/province/list")
	public String provinceList(Model model) {
		return "useStatistics/provinceList";
	}

	/**
	 * 
	 * 按省份统计列表数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/province/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> provinceListJson(DataGridModel page, UseStatisticsDto usDTO) {
		String startTime = usDTO.getStartTime();
		String endTime = usDTO.getEndTime();
		if (!StringUtils.isBlank(startTime)) {
			usDTO.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			usDTO.setEndTime(endTime.replace("-", ""));
		}
		return useStatisticsService.findProvinceListWithPage(page, usDTO);
	}

	/**
	 * 
	 * 用户统计图表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/chart")
	public String chart(Model model) {
		List<BaseInfo> baseInfos = baseInfoService.getAllChannelInfo();
		model.addAttribute("baseInfos", baseInfos);
		return "useStatistics/chart";
	}

	/**
	 * 用户统计图表数据
	 * 
	 * @param model
	 * @param issueStatisticsVo
	 * @return
	 */
	@RequestMapping(value = "/chartData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(Model model, UseStatisticsDto usDTO) {
		String startTime = usDTO.getStartTime();
		String endTime = usDTO.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			usDTO.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YYMMDD, 30));
		}
		if (!StringUtils.isBlank(startTime)) {
			usDTO.setStartTime(startTime.replaceAll("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			usDTO.setEndTime(endTime.replaceAll("-", ""));
		}

		Map<String, Object> map = useStatisticsService.selectRecords(usDTO);
		return map;
	}

	/**
	 * 用户统计图表-音乐、阅读、灵犀
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/chartPart")
	public String chartPart(Model model) {
		return "useStatistics/chartPart";
	}

	/**
	 * 用户统计图表数据-音乐、阅读、灵犀
	 * 
	 * @param model
	 * @param usDTO
	 * @return
	 */
	@RequestMapping(value = "/chartPartData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listPart(Model model, UseStatisticsDto usDTO) {
		String startTime = usDTO.getStartTime();
		String endTime = usDTO.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			usDTO.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YYMMDD, 30));
		}
		if (!StringUtils.isBlank(startTime)) {
			usDTO.setStartTime(startTime.replaceAll("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			usDTO.setEndTime(endTime.replaceAll("-", ""));
		}

		Map<String, Object> map = useStatisticsService.selectPartRecords(usDTO);
		return map;
	}

}
